package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.ChessBoard;
import com.sghd.fight.module.game.core.Grid;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author frank
 * @date 2018-12-13 14:06
 * @desc 暗愈术
 **/
@Component
public class DarkMedicineEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        ChessBoard chessBoard = room.getChessBoard();
        Grid[] grids = chessBoard.getGrids();
        ChessType source = ChessType.valueOf(ctx.get(SOURCE).toString());
        //消除的宝石
        Set<Integer> eliminate = new HashSet<>();
        for (Grid grid : grids) {
            if (source == grid.getType()) {
                eliminate.add(grid.getIndex());//记录
                grid.remove();
            }
        }
        if (!eliminate.isEmpty()) {
            int recover = eliminate.size();//恢复量
            int level = owner.getSkillLevel(config.getId());
            Map<String, Object> recover2Level = MapUtils.getMap(ctx, String.valueOf(level));
            recover += MapUtils.getIntValue(recover2Level , RECOVER);//技能等级额外提供恢复
            recover += getAddedHp(owner, room,config.getType());
            context.addValue(target, UnitValue.HP, recover);//加血
            targetReport.addValue(ValueItem.valueOf(UnitValue.HP, recover));

            chessBoard.getEliminated().addAll(eliminate);
            chessBoard.fill();
            //自动消除
            autoElimi(room, owner, skillReport, chessBoard);
        }
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_DARK_MEDICINE;
    }
}
